package com.jxc.jxc01.yk.web;

import com.alibaba.fastjson.JSONObject;
import com.jxc.jxc01.yk.dao.SalesDaoImpl;
import com.jxc.jxc01.yk.entity.PageHelper;
import com.jxc.jxc01.yk.entity.RespResult;
import com.jxc.jxc01.yk.entity.Sales;
import com.jxc.jxc01.yk.entity.SalesReturn;
import com.jxc.jxc01.yk.service.ISalesReturnService;
import com.jxc.jxc01.yk.service.SalesReturnService;
import com.jxc.jxc01.yk.util.UUIDCode;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet("*.re")
public class SalesReturnServlet extends HttpServlet {
    ISalesReturnService service = new SalesReturnService();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        //获取请求地址
        String uri = request.getRequestURI();
        uri = uri.substring(uri.lastIndexOf("/") + 1, uri.lastIndexOf("."));

        if (uri.equals("ListSalesReturn")) {
            System.out.println("进入到=》ListSalesReturn");
            ListSalesReturn(request, response);
        }  else if (uri.equals("addSalesReturn")) {
            System.out.println("进入到=》addSalesReturn");
            addSalesReturn(request, response);
        }else if (uri.equals("findSalesReturn")) {
            System.out.println("进入到=》findSalesReturn");
            findSalesReturn(request, response);
        }
    }

    /**
     * 添加销售退货信息
     */
    private void addSalesReturn(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<SalesReturn> salesReturns = new ArrayList<>();
        //RespResult result = null;
        SalesReturn salesReturn = new SalesReturn();
        //获取要增加的销售退货信息
        String salesnumberstr = request.getParameter("salesnumber");
        if (salesnumberstr == null || salesnumberstr==""){
            salesnumberstr = "0";
        }
        int salesnumber = Integer.parseInt(salesnumberstr);
        //查询是否存在此销售单号
        SalesDaoImpl salesDao = new SalesDaoImpl();
        List<Sales> sales = salesDao.findSalesSalesNumber(salesnumber);
        if (sales == null || sales.size() == 0){
            //销售单号不存在
            salesReturns = new ArrayList<>();
            response.sendRedirect("xiaoshoutuihuo.html");
            return;
        }
        salesReturns = service.findSalesReturnumber(salesnumber);

        salesReturn.setSalesnumber(salesnumber);
        //生成一个uuid销售退货单号
        int goods_returned_num = new UUIDCode().getUuid();
        salesReturn.setGoods_returned_num(goods_returned_num);
        String goods_returned_countStr = request.getParameter("goods_returned_count");
        if (goods_returned_countStr.equals(null) || goods_returned_countStr.equals("")){
            goods_returned_countStr = "0";
        }
        salesReturn.setGoods_returned_count(Integer.parseInt(goods_returned_countStr));
        String goods_returned_priceStr = request.getParameter("goods_returned_price");
        if (goods_returned_priceStr.equals(null) || goods_returned_priceStr.equals("")){
            goods_returned_priceStr = "0";
        }
        salesReturn.setGoods_returned_price(Double.parseDouble(goods_returned_priceStr));
        //salesReturn.setGoods_returned_count(Integer.parseInt(request.getParameter("goods_returned_count")));
        //salesReturn.setGoods_returned_price(Integer.parseInt(request.getParameter("goods_returned_price")));
        salesReturn.setGoods_returned_total(salesReturn.getGoods_returned_count() * salesReturn.getGoods_returned_price());
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String goods_returned_time = sdf.format(date);
        salesReturn.setGoods_returned_time(goods_returned_time);
        salesReturn.setGoods_returned_reason(request.getParameter("goods_returned_reason"));
        salesReturn.setCondition(request.getParameter("status"));
        salesReturn.setComment(request.getParameter("comment"));
        salesReturns.add(salesReturn);
        boolean addSalesReturn = service.addSalesReturn(salesReturn);
        response.sendRedirect("xiaoshoutuihuo.html");
    }

    /**
     * 根据销售单号查询销售退货信息
     */
    private void findSalesReturn(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        List<SalesReturn> salesReturns = new ArrayList<>();
        RespResult result = null;
        //获取要查找的销售单号
        String salesnumberstr = request.getParameter("salesnumber");
        if (salesnumberstr == null || salesnumberstr==""){
            salesnumberstr = "0";
        }
        int salesnumber = Integer.parseInt(salesnumberstr);
        salesReturns = service.findSalesReturnumber(salesnumber);
        result =  new RespResult(0, "ok", salesReturns.size(), salesReturns);
        HttpSession session = request.getSession();
        session.setAttribute("findOutWarehouseNumber", result);
        out.println(JSONObject.toJSONString(result));
        response.sendRedirect("xiaoshoutuihuo.html");
    }



    /**
     * 查询所有销售退货记录
     */
    private void ListSalesReturn(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        RespResult findOutWarehouseNumber = (RespResult) session.getAttribute("findOutWarehouseNumber");
        if (findOutWarehouseNumber == null) {
            List<SalesReturn> salesReturns = new ArrayList<>();
            PageHelper<SalesReturn> pageHelper = new PageHelper();
            //查询出库记录
            salesReturns = service.ListSalesReturn(pageHelper);
            RespResult result = new RespResult(0, "ok", pageHelper.getTotal(), salesReturns);
            out.print(JSONObject.toJSONString(result));
        } else {
            out.print(JSONObject.toJSONString(findOutWarehouseNumber));
            session.removeAttribute("findOutWarehouseNumber");
        }
    }

}
